package com.baizhi.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.auth0.jwt.interfaces.JWTVerifier;

import java.util.Date;
import java.util.Map;

public class JwtUtil {
    private static final String SING="1Q2W#e$r";
    public static String getToken(Map<String,String> map) {
        // 设置过期时间
        Date date = new Date(System.currentTimeMillis() + 1800000);
        // 私钥和加密算法
        Algorithm algorithm = Algorithm.HMAC256(SING);
        // 返回token字符串
        JWTCreator.Builder builder = JWT.create();
        map.forEach((k,v)->{
            builder.withClaim(k,v);
        });
        String token = builder.withExpiresAt(date).sign(algorithm);
        return token;
    }
    public static DecodedJWT verify(String token){
        Algorithm algorithm = Algorithm.HMAC256(SING);
        JWTVerifier verifier = JWT.require(algorithm).build();
        DecodedJWT jwt = verifier.verify(token);
        return jwt;
    }
}
